//
//  NewFeatureViewController.swift
//  Weobo
//
//  Created by 史亚洲 on 16/1/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


//设置新界面图片数量
private let imageCount=4

// 'UICollectionView must be initialized with a non-nil layout parameter'
// 集合视图  最常见的错误
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    //构造方法,方法只能够继承上一个层次,而不能再继续向上面继承
    init() {
    
        //1.调用指定的构造方法,实例化对象,指定流水布局
        let layout=UICollectionViewFlowLayout()
        
        //2.修改布局
        layout.itemSize=UIScreen.mainScreen().bounds.size
        
        //3.修改视图滚动的方法
        layout.scrollDirection=UICollectionViewScrollDirection.Horizontal
        
        //4.设置上下左右间距
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
        
        
        //super.init()
        super.init(collectionViewLayout: layout)
        

        
        //5.开启分页效果
        collectionView?.pagingEnabled=true
        
        //6.隐藏水平显示器
        collectionView?.showsHorizontalScrollIndicator=false
              //7.取消弹簧效果
        collectionView?.bounces=false

    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
            super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource




    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
        
        //测试代码
       // cell.backgroundColor=indexPath.row % 2 == 0 ? UIColor.redColor() :UIColor.greenColor()
        cell.index=indexPath.item
        
        return cell
    }

    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }

    override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
        return false
    }

    override func collectionView(collectionView: UICollectionView performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    
    }
    */

}

//在同一个文件中可以同时建立多个类
class NewFeatureCell: UICollectionViewCell {
    
    //1.添加index属性
    var index: Int = 0 {
    
        didSet {
            
            /**
            *  重写set方法
            */
            iconImageView.image=UIImage(named: "new_feature_\(index+1)")
            
            //注意:隐藏开始按钮
     
            //startBtn.hidden = (index != 3)
            if index==3{
            
            startBtn.hidden=false
            showAnimation()
            
            }else {
            
            startBtn.hidden=true
            }
           
        }
    
    
    
    }
    
    //开始按钮执行动画效果
    private func showAnimation() {
        
        //在开始动画之前,修改transform
        startBtn.transform=CGAffineTransformMakeScale(0, 0)
        //在动画的闭包中,修改按钮的大小
        /*
        衰变的动画效果(iOS7.0以后推出的)
        
        1.- parameter <Tduration:             动画持续时间
        2.- parameter delay:                  动画延迟时间
        3.- parameter usingSpringWithDamping: 弹簧系数 区间 0.1 ~ 1  越小越弹
        4.- parameter initialSpringVelocity:  加速度
        5.- parameter options:                动画的可选项  swift中使用数组
        6.- parameter animations:             执行动画闭包
        7.- parameter completion:             动画执行完成的闭包
        
        
        
        */
        startBtn.hidden=false
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.startBtn.transform=CGAffineTransformIdentity
            }) { (_) -> Void in
                print("完成动画")
        }

    
    }
    @objc func startBtnClick() {
    
        print(__FUNCTION__)
        
        //动画结束,切换动画
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
    
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
 /// 设置UI界面
    private func setupUI() {
    
        //设置约束
        contentView.addSubview(iconImageView)
        contentView.addSubview(startBtn)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
        startBtn.addTarget(self, action: "startBtnClick", forControlEvents: .TouchUpInside)
    
    
    }
    
     // 分析视图中的所有的子视图 将所有的子视图懒加载出来
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    
    //懒加载开始按钮
    private lazy var startBtn: UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        
        
        btn.sizeToFit()
        return btn
    
    }()


}
